package com.totoro.tyrank.manager;

import com.totoro.tyrank.constants.StaticSystemVarConst;
import com.totoro.tyrank.data.ConfigData;
import com.totoro.tyrank.data.MySQLData;
import com.totoro.tyrank.utils.ColorUtil;
import com.totoro.tyrank.utils.ListUtil;
import lombok.Getter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 针对config配置的管理器
 */
public class ConfigManager {

    private final Plugin plugin;
    @Getter
    private ConfigData configData;

    public ConfigManager(Plugin plugin) {
        configData = new ConfigData();
        this.plugin = plugin;
    }

    public void createConfig() {
        if (!this.plugin.getDataFolder().exists()){
            this.plugin.getDataFolder().mkdirs();
            this.plugin.saveDefaultConfig();
        }
    }

    public void reloadConfig() {
        plugin.reloadConfig();
        FileConfiguration config = plugin.getConfig();
        boolean debug = config.getBoolean("debug", false);
        String prefix = ColorUtil.color(config.getString("prefix", "&6[TyRank]"));
        String playerIdentity = config.getString("player-identity", "UUID");
        String useAttribute = config.getString("use-attribute", "ap");
        String dateFormat = config.getString("date-format", "yyyy-MM-dd HH:mm:ss");
        Long clickCooldown = config.getLong("click-cooldown", 1000);
        // 上线提示
        ConfigData.OnlineTipsEnable onlineTipsEnable = configData.new OnlineTipsEnable(false, false);
        ConfigurationSection onlineTipsEnableSection = config.getConfigurationSection("online-tips-enable");
        if(onlineTipsEnableSection != null) {
            onlineTipsEnable.setAttribute(onlineTipsEnableSection.getBoolean("attribute", false));
            onlineTipsEnable.setReward(onlineTipsEnableSection.getBoolean("reward", false));
        }
        String endMessage = ColorUtil.color(config.getString("end-message", ""));
        List<String> endPlayerAction = config.getStringList("end-player-action").stream().map(ColorUtil::color).collect(Collectors.toList());
        if(ListUtil.isBlank(endPlayerAction)) {
            endPlayerAction = StaticSystemVarConst.EMPTY_LIST;
        }
        configData.setDebug(debug);
        configData.setPrefix(prefix);
        configData.setPlayerIdentity(playerIdentity);
        configData.setUseAttribute(useAttribute);
        configData.setDateFormat(dateFormat);
        configData.setClickCooldown(clickCooldown);
        configData.setOnlineTipsEnable(onlineTipsEnable);
        configData.setEndMessage(endMessage);
        configData.setEndPlayerAction(endPlayerAction);
    }

    public void initMySQL() {
        plugin.reloadConfig();
        FileConfiguration config = plugin.getConfig();
        boolean mysqlEnable = config.getBoolean("mysql.enable", false);
        MySQLData mySQLData = new MySQLData();
        mySQLData.setEnable(mysqlEnable);
        if(mysqlEnable) {
            String databaseName = config.getString("mysql.databasename");
            String userName = config.getString("mysql.username");
            String password = config.getString("mysql.password");
            String port = config.getString("mysql.port");
            String ip = config.getString("mysql.ip");
            mySQLData = new MySQLData(true, databaseName, userName, password, port, ip);
        }
        configData.setMySQLData(mySQLData);
    }


}
